<!-- 首页 -->
<template>
	<view>
		<tn-custom bgColor="bg-gradual-pink" :isBack="false">
			<block slot="left">
				<view class="btn-left" @click="shaixuan">
					<u-icon name="shaixuan" custom-prefix="iconfont"></u-icon>
					<view class="left-text">筛选</view>
				</view>
			</block>
			<block slot="content">
				<view class="tab-box">
					<view class="content-top">
						<view :class="curNow == 1 ? 'content-btn' : 'content-btn1' " @click="onclick(1)">推荐</view>
						<view :class="curNow == 2 ? 'content-btn' : 'content-btn1' " @click="onclick(2)">附近</view>
					</view>
				</view>
			</block>
		</tn-custom>
		<card v-if="curNow == 1" ref="last"></card>
		<near v-if="curNow == 2" ref="near"></near>
		<coin-popup></coin-popup>
		<user-auth></user-auth>
		<user-phone></user-phone>
		<user-pay></user-pay> 
		<cash-popup></cash-popup> 
		<search-popup ref="search" @query="search"></search-popup>
	</view>
</template>

<script>
import card from "./card.vue"; // 推荐
import near from "./near.vue"; // 附近
export default {
	components: {
		card,
		near,
	},
	data() {
		return {
			curNow: 0,
			list: [
				{
					name: '推荐'
				}, 
				{
					name: '附近'
				}, 
			],
			flag: false,
			userId: 0,
		};
	},
	created() {
		uni.$on('indexhome',this.query);
	},
	destroyed() {
		// 注销全局配置监听
		uni.$off('indexhome');
	},
	methods: {
		onclick (data) {
			if(data == 2){
				this.handerChooseLocation();
			}else{
				this.curNow = data;
			}
		},
		handerChooseLocation () {
		  uni.chooseLocation({
		    success: (res) => {
		  	  this.updateLocation(res.latitude, res.longitude);
		    },
		    fail: function (err) {
		        console.log('取消按钮', err)
		    }
		  });
		},
		updateLocation(lat,lng) {
			this.$u.put('/system/user/profile',{
				lat: lat,
				lon: lng,
			}).then(res => {
				this.curNow = 2;
			})
		},
		sectionChange(index) {
			this.curNow = index;
		},
		shaixuan() {
			this.judgeLogin(() => {
				this.$refs.search.open();
			});
		},
		search() {
			if(this.curNow == 1){
				this.$refs.last.query();
			}else if(this.curNow == 2){
				this.$refs.near.query();
			}
		},
		query(e) {
			this.curNow = 1;
			this.$refs.last.query();
		},
	}
};
</script>
<style lang="scss" scoped>
	page {
		background-color: #fff;
	}
	.top-box {
		display: flex;
		background-color: #fff;
		align-items: center;
		height: 40px;
		justify-content: space-between;
	}
	.u-tabs-list {
		display: flex;
		align-items: center;
		flex: 1;
	}
	.u-tabs-box {
		display: flex;
		flex: 1;
		align-items: center;
		justify-content: center;
	}
	.u-tabs {
		padding: 5px 0;
		font-size: 28rpx;
		display: flex;
	}
	.u-tabs-active {
		font-weight: bolder;
		color: #a569d9;
		border-bottom: 2px solid #a569d9;
	}
	.amount {
		font-size: 26rpx;
		margin-right: 15px;
		font-weight: bolder;
		line-height: 80rpx;
		color: #2979ff;
	}
	.user-box {
		display: flex;
		align-items: center;
		padding: 15px;
		padding-bottom: 0;
	}
	.user-name-box {
		display: flex;
		align-items: center;
	}
	.nickname {
		font-size: 28rpx;
		margin-right: 5px;
	}
	.sex-box {
		background-color: #2b85e4;
		display: flex;
		color: #fff;
		padding: 4rpx 10rpx;
		border-radius: 20px;
		font-size: 16rpx;
	}
	.sex-box-nv {
		background-color: #fe709f;
		display: flex;
		color: #fff;
		padding: 4rpx 10rpx;
		border-radius: 20px;
		font-size: 16rpx;
	}
	.user-tag-box {
		display: flex;
		align-items: center;
	}
	.user-tag {
		font-size: 20rpx;
		color: #82848a;
		margin: 10rpx 0;
	}
	.user-box-right {
		margin-left: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex: 1;
	}
	.sign-name {
		font-size: 24rpx;
		color: #8d8d8d;
		text-overflow: ellipsis;
		width: 400rpx;
		overflow: hidden;
		white-space: nowrap;
	}
	.line {
		float:left;
		width: 1px;
		height: 16rpx; 
		background: #e5e5e5;
		margin: 0 5px;
	}
	.loadmore-box {
		padding: 15px;
	}
	.btn-left {
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #e6028e;
		margin-left: 20px;
		padding: 8rpx 20rpx;
		border-radius: 20px;
	}
	.left-text {
		margin-left: 10rpx;
		font-size: 28rpx;
	}
	
	.tab-box {
		margin: 0 40rpx;
		font-size: 28rpx;
		border-radius: 20px;
		padding: 1px 2px;
		background-color: #F4F4F4;
		.content-top {
			color: #ffffff;
			background-color: #F4F4F4;
			border-radius: 50rpx;
			display: flex;
			width: 260rpx;
			justify-content: space-between;
			align-items: center;
			height: 55rpx;
			.content-btn {
				display: flex;
				flex: 1;
				justify-content: center;
				align-items: center;
				background-color: #FF71D2;
				height: 100%;
				border-radius: 50rpx;
				font-weight: 600;
				animation:myfirst 0.3s;
				@keyframes myfirst
				{
					0% {margin: 0rpx -10rpx;}
					50% {margin: 0rpx 10rpx;}
					100% {margin: 0rpx;}
				}
			}
			.content-btn1 {
				display: flex;
				flex: 1;
				justify-content: center;
				align-items: center;
				height: 100%;
				border-radius: 50rpx;
				color: #82B2FF;
			}
		}
		
		animation:myfirst 0.3s;
		@keyframes myfirst
		{
			0% {margin: 0rpx -10rpx;}
			50% {margin: 0rpx 10rpx;}
			100% {margin: 0rpx;}
		}
	}
</style>
